package graph_viewer.factory;

import java.io.File;
import java.io.IOException;

import graph_viewer.structure.*;
import graph_viewer.structure.builder.*;
import graph_viewer.app_exception.*;
import graph_viewer.util.*;


public class  Graph_Factory{
	private Tree	tree;
	
	public Graph_Factory(){
		tree	= new Generic_Tree();
	}

	public Graph_Factory( Tree a_tree ){
		tree = a_tree;
	}

	public Graph create( String file_name ){
		return create( file_name, Visual_Component.CLUSTERED_GRAPH );
	}

	public Graph create( String file_name, String graph_type ){
		Graph_Builder		gb		= null;
		Graph				a_graph	= null;
		
		Graph_Builder_Factory	gbf	= new Graph_Builder_Factory();

		try{
			gb		= gbf.create( file_name );
			
			Debug.println(  "Graph_Builder is created in Graph_Factory!" + gb, 4 );
			if ( Structure_Util.is_tree( graph_type ) ){
				a_graph	= gb.build_tree( tree );
			}else if ( Structure_Util.is_clustered_graph( graph_type ) ){
				a_graph	= gb.build_clustered_graph( tree );
			}		

			Out.println(  "\nGraph_Factory build a " + graph_type + " from:\t" + gb.get_file() + "\n");

		}catch ( IOException ioe ){
			Out.println( ioe.toString() );			
			Out.println( "IOException: Graph_Factory failed to build a " + graph_type + " from " + gb.get_file() );
		
		}catch ( Exception e ){
			Out.println( e.toString() );			
			Out.println( "Exception: Graph_Factory failed to build a " + graph_type + " from " + gb.get_file() );			
		}

		return a_graph;

	}	
}